package com.wu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.mapper.CommentManagementMapper;
import com.wu.mapper.CommentsMapper;
import com.wu.pojo.model.CommentManagement;
import com.wu.pojo.model.UserToken;
import com.wu.service.CommentManagementService;
import com.wu.util.MyUtil;
import com.wu.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.wu.constant.Constant.*;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-07 23:10
 */
@Service
public class CommentManagementServiceImpl extends ServiceImpl<CommentManagementMapper, CommentManagement> implements CommentManagementService {
    @Autowired
    private CommentManagementMapper commentManagementMapper;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Map<String, Object> getCommentManagementByUserId(int curPage) {
        Map<String, Object> map = new HashMap<>();
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        //查询总评论数，用来分页查询
        Integer count = commentsMapper.getCountByUserId(userId);
        int totalPage = (count + 15 - 1) / 15;
        if (totalPage == 0) {
            map.put(TOTAL_PAGE, 0);
            map.put(COLLECT, new ArrayList<>());
            return map;
        }
        if (curPage < 1) {
            curPage = 1;
        }
        if (curPage > totalPage) {
            curPage = totalPage;
        }
        map.put(TOTAL_PAGE, totalPage);
        long longTime = System.currentTimeMillis();
        List<CommentManagement> comments = commentManagementMapper.getCommentManagementByUserId(userId, (curPage - 1) * 15);
        return getStringObjectMap(map, longTime, comments);
    }

    private Map<String, Object> getStringObjectMap(Map<String, Object> map, long longTime, List<CommentManagement> comments) {
        List<CommentManagement> collect = comments.stream().peek(comment -> {
            String commentDate = comment.getCommentDate();
            long commentTime = myUtil.parseTimeToString(commentDate);
            long time = longTime - commentTime;
            String overdueTime = "";
            long howTime = time / 1000 / 60;
            if (howTime < 60) {
                overdueTime = howTime + "分钟前";
            } else if (howTime < 1440) {
                overdueTime = howTime / 60 + "小时前";
            } else if (howTime < 43200) {
                overdueTime = howTime / 1440 + "天前";
            } else if (howTime < 15768000) {
                overdueTime = howTime / 43200 + "月前";
            } else {
                overdueTime = howTime / 15768000 + "年前";
            }
            comment.setOverdueTime(overdueTime);
        }).collect(Collectors.toList());
        map.put(COLLECT, collect);
        return map;
    }

    @Override
    public Map<String, Object> getOwnerComments(int curPage) {
        Map<String, Object> map = new HashMap<>();
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        //查询总评论数，用来分页查询
        Integer count = commentsMapper.getOwnerCommentCount(userId);
        map.put(COUNT, count);
        int totalPage = (count + 15 - 1) / 15;
        if (totalPage == 0) {
            map.put(TOTAL_PAGE, 0);
            map.put(COLLECT, new ArrayList<>());
            return map;
        }
        if (curPage < 1) {
            curPage = 1;
        }
        if (curPage > totalPage) {
            curPage = totalPage;
        }
        map.put(TOTAL_PAGE, totalPage);
        long longTime = System.currentTimeMillis();
        List<CommentManagement> comments = commentManagementMapper.getOwnerComment(userId, (curPage - 1) * totalPage);
        return getStringObjectMap(map, longTime, comments);
    }

    @Override
    public Map<String, Object> getAllNotPassCommentsByUserId(int curPage) {
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        Map<String, Object> map = new HashMap<>();
        Integer count = commentManagementMapper.getNotPassCommentCount(userId);
        map.put(COUNT, count);
        int totalPage = (count + 15 - 1) / 15;
        if (totalPage == 0) {
            map.put(TOTAL_PAGE, 0);
            map.put(NOT_PASS_COMMENT, new ArrayList<>());
            return map;
        }
        if (curPage < 1) {
            curPage = 1;
        }
        if (curPage > totalPage) {
            curPage = totalPage;
        }
        List<CommentManagement> notPassComment = commentManagementMapper.getALlNotPassComment(userId, (curPage - 1) * totalPage);
        map.put(TOTAL_PAGE, totalPage);
        map.put(NOT_PASS_COMMENT, notPassComment);
        return map;
    }

    @Override
    public boolean tokenOpenOrClose() {
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        if (redisTemplate.opsForSet().isMember(IS_ENABLE_COMMENT_AUDIT, userId)) {
            redisTemplate.opsForSet().remove(IS_ENABLE_COMMENT_AUDIT, userId);
        } else {
            redisTemplate.opsForSet().add(IS_ENABLE_COMMENT_AUDIT, userId);
        }
        return redisTemplate.opsForSet().isMember(IS_ENABLE_COMMENT_AUDIT, userId);
    }

    @Override
    public boolean tokenGetOpenOrClose() {
        UserToken userToken = tokenUtils.parseTokenToUserToken();
        String userId = userToken.getUserId();
        return redisTemplate.opsForSet().isMember(IS_ENABLE_COMMENT_AUDIT, userId);
    }
}
